package com.blog.controller;

import com.blog.entity.RestBean;
import com.blog.entity.article.ArticleType;
import com.blog.service.ArticleTypeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/type")
public class ArticleTypeController {

    @Resource
    ArticleTypeService service;

    @GetMapping("/all")
    public RestBean<List<ArticleType>> all() {
        return RestBean.success(service.getAllArticleTypes());
    }

    @GetMapping("/tid")
    public RestBean<ArticleType> tid(Integer tid) {
        return RestBean.success(service.getArticleTypeByTid(tid));
    }

    @PostMapping("/add")
    public RestBean<String> add(String type) {
        String s = service.addArticleType(type);
        return s == null ? RestBean.success("新增类型成功") : RestBean.failure(400, s);
    }

    @PostMapping("/update")
    public RestBean<String> update(ArticleType articleType) {
        String s = service.updateArticleType(articleType);
        return s == null ? RestBean.success("修改类型成功") : RestBean.failure(400, s);
    }

    @PostMapping("/delete")
    public RestBean<String> delete(Integer tid) {
        String s = service.deleteArticleType(tid);
        return s == null ? RestBean.success("删除类型成功") : RestBean.failure(400, s);
    }
}
